package com.rose.doorto.common.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Formatter;

/**
 * @Author 李庆超
 * @email 1481791489@qq.com
 * @Package：com.rose.doorto.common.util
 * @Project：door-to-door
 * @name：NetworkInfo
 * @date 2024/07/10 2024/7/10:16:41:
 */
public class NetworkInfo {

    public static void main(String[] args) {
        try {
            // 获取本机的InetAddress实例
            InetAddress ip = InetAddress.getLocalHost();
            String ipAddress = ip.getHostAddress(); // 获取IP地址
            System.out.println("IP Address: " + ipAddress);

//            // 获取网络接口对象，并遍历
//            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
//            while (interfaces.hasMoreElements()) {
//                NetworkInterface networkInterface = interfaces.nextElement();
//                byte[] mac = networkInterface.getHardwareAddress();
//                if (mac != null) {
//                    Formatter formatter = new Formatter();
//                    for (byte b : mac) {
//                        formatter.format("%02X:", b);
//                    }
//                    formatter.close();
//                    String macAddress = formatter.toString();
//                    macAddress = macAddress.substring(0, macAddress.length() - 1); // 移除最后一个冒号
//                    System.out.println("MAC Address: " + macAddress);
//                }
//            }
        } catch (Exception e) {
            e.printStackTrace();
       }
    }
}
